package face.pay.common.user.service;

import face.pay.constant.UserType;
import face.pay.dao.*;
import face.pay.entity.*;
import face.pay.util.ResultMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Auther: zkj
 * @Date: 2019/7/5 11:25
 * @Description:用户注册基础服务
 */
@Component
public class UserRegisterBiz {

   @Autowired
   private  UserInfoDao userInfoDao;

   @Autowired
   private RoleInfoDao  roleInfoDao;

   @Autowired
   private EnterpriseInfoDao enterpriseInfoDao;


   @Autowired
   private UserRoleInfoDao userRoleInfoDao;

   @Autowired
   private OemInfoDao oemInfoDao;

    @Autowired
   private  UserBankInfoDao userBankInfoDao;

    @Autowired
   private  UserAccountDao  userAccountDao;


    /**
     * 用户注册  事务交给调用层处理
     * @param roleCode
     * @param userInfo
     * @return
     */
   public  Boolean registUserWithRole(String roleCode, UserInfo userInfo){


       RoleInfo roleInfo =  this.roleInfoDao.getUserRoleByCode(roleCode);

       if(roleInfo!=null){

           Integer roleId = roleInfo.getId();
           /**
            * 指定默认密码
            */
           if(StringUtils.isBlank(userInfo.getPassWord())){
               userInfo.setPassWord("888888");
           }


           String passWord = new BCryptPasswordEncoder().encode(userInfo.getPassWord());

           String userName = userInfo.getUserName().trim();
           userInfo.setUserName(userName);
           userInfo.setPassWord(passWord);

           String userType = userInfo.getType();

           if(UserType.ENTERPRISE.equals(userType)){

               Integer agencyId = userInfo.getAgencyId();
               Integer recommendId= userInfo.getRecommendId();
               if(agencyId==null){
                   userInfo.setAgencyId(userInfo.getRecommendId());
               }
               if(recommendId==null){
                   userInfo.setRecommendId(userInfo.getAgencyId());
               }
           }

           this.userInfoDao.insert(userInfo);

           Integer userId = userInfo.getId();

           this.userRoleInfoDao.insert(userId,roleId);

           /**
            * 插入银行卡
            */
           if (!"ROLE_COMPANY_MANAGER".equals(roleCode)) {

               UserBankInfo userBankInfo  =  new UserBankInfo(userInfo.getOemId(),userId);

               this.userBankInfoDao.insert(userBankInfo);


               UserAccount userAccount =  new UserAccount(userInfo.getOemId(),userId);

               this.userAccountDao.insert(userAccount);
           }

           if(UserType.ENTERPRISE.equals(userType)){
               /**
                * 绑定商家
                */
               EnterpriseInfo enterpriseInfo =  new EnterpriseInfo();

               enterpriseInfo.setUserId(userId);
               enterpriseInfo.setOemId(userInfo.getOemId());
               this.enterpriseInfoDao.insert(enterpriseInfo);

           }

           return  true;
       }


       return  false;
   }

   public  void updateUser(UserInfo userInfo){
        this.userInfoDao.update(userInfo);

   }

    /**
     * 需要判断当前OEM 上古下有没有该用户 注册
     */
   public  Boolean validate(UserInfo userInfo){

       Integer oemId = userInfo.getOemId();

       Integer count =  this.userInfoDao.countByOemAndUserName(oemId,userInfo.getUserName());
       return count <= 0;
   }

   public List<UserInfo> queryList(UserInfo userInfo){
       return  this.userInfoDao.queryDetailList(userInfo);

   }

    public UserInfo login(UserInfo userInfo,String domain,String userType){
        OemInfo oemInfo=new OemInfo();
        if(UserType.AGENCY.equals(userType)){
            oemInfo.setAgencyDomain(domain);
        }else{
            oemInfo.setEnterpriseDomain(domain);
        }

        OemInfo oem=this.oemInfoDao.getOemInfo(oemInfo);

        if(oem!=null){
            userInfo.setType(userType);
            userInfo.setOemId(oemInfo.getId());
            userInfo.setDeleted(0);
            UserInfo loginUser=this.userInfoDao.queryUser(userInfo);

            if(loginUser!=null){
                /***
                 *
                 */
                 Boolean success =  new BCryptPasswordEncoder().matches(userInfo.getPassWord(),loginUser.getPassWord());

                 if(success){
                     return  loginUser;
                 }

            }


        }


       return  null;
   }


    public ResultMessage modifyPassword(UserInfo loginUser,  String newPassword){
            UserInfo userInfo = this.userInfoDao.getById(loginUser.getId());
            userInfo.setPassWord(new BCryptPasswordEncoder().encode(newPassword));
            this.userInfoDao.updatePassword(userInfo);
            return  ResultMessage.createSuccess();


    }


}
